$(function () {
    /**初始化人员选择**/
    init_selectReceUser();
    /**初始化时间选择**/
    $("#select_time span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));
    $('#select_time').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#select_time span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $JuXue.refreshTable("saleCusttransTable");
    });
    /**列表查询**/
    var settings = {
        url: ctx + "cusDynamic/getAllCusTransData",
        pageList: [10,50, 100],
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                dyCreator   :  $("#geren").val(),   //跟进人
                starttime : $("#starttime").val(), //开始时间
                endtime   : $("#endtime").val()    //结束时间
            };
        },
        columns: [{
            checkbox: true,
        }, {
            field: 'dyDisName',
            title: '客户名称',
            clickToSelect:false,
            formatter:function (value, row, index) {
                return "<a href='javascript:void(0)' onclick='viewCusReceDetail(this);' uuid = '"+row.dyCusUuid+"' data='"+row.dySourceType+"'>"+value+"</a>";
            }
        }, {
            field: 'dyCreatorName',
            title: '转让人',
        }, {
            field: 'dyZhuanName',
            title: '接收人',
        },{
            field: 'dyCreattime',
            title: '领取时间',
        },{
            field: 'endtime',
            title: '创建时间',
        }]
    };
    $JuXue.initTable('saleCusttransTable', settings);
});
/**切换时间类型**/
function selectReceSeletTimne(btn) {
    var data_cliak = $(btn).attr("data-click");
    if(data_cliak === "selft"){
        $("#select_time").removeClass("hidden");
        $("#starttime").val(moment().subtract("days",29).format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }else{
        $("#select_time").addClass("hidden");
        $("#starttime").val('')
        $("#endtime").val('');
    }
    $("#timeRemove").find("button").each(function () {
        $(this).removeClass("btn-primary");
    });
    $(btn).addClass("btn-primary");
    $JuXue.refreshTable('saleCusttransTable');
}

/**初始化选择人员**/
function init_selectReceUser(){
    $.post(ctx+"dept/getUserAndDeptByTreeWhisRole",{},function (r) {
        var array  = r.msg;
        $("#select_Rece_user").jstree({
            "core": {
                "multiple": true,//单选
                "data":array,
                "icon":false,
                "themes":{
                    "icons":false
                }
            },
            "checkbox" : {
                "keep_selected_style" : false
            },
            "plugins" : [ "wholerow", "checkbox" ]
        });
        // 获得点击节点的id
        $('#select_Rece_user').on("changed.jstree", function (e, data) {
            var nodeid = data.node.id;
            var text = data.node.text;
            var action = data.action;;
            if (action == "deselect_node") {
                $("#otherUserSelect").find("div").each(function () {
                    var data_id = $(this).attr("data-id");
                    if (data_id == nodeid) $(this).remove();
                });
            }else{
                /**判断是不是超过 3个 超过3个则不能继续追加**/
                var divlength = $("#otherUserSelect").find("div");
                if(divlength.length >= 3){
                    $JuXue.n_danger("选择查询类型不能超过3个");
                    $("#select_Rece_user").jstree("deselect_node",nodeid);
                    return;
                }else{
                    if(divlength.length >0){
                        var ble = false;
                        $("#otherUserSelect").find("div").each(function () {
                            var data_id = $(this).attr("data-id");
                            if (data_id == nodeid){ble = true;}
                        });
                        if(!ble){
                            var label = "<div class='label label-danger p-3 m-3 ' data-id='"+nodeid+"'>"+text+"</div>";
                            $("#otherUserSelect").append(label);
                        }
                    }else{
                        var label = "<div class='label label-danger p-3 m-3 ' data-id='"+nodeid+"'>"+text+"</div>";
                        $("#otherUserSelect").append(label);
                    }
                }
            }
        });
    });
}
/**确定人员选择**/
function selectReceUserList(){
    $('#selectReceUsers').removeClass('open');
    var allUser = new Array();//$('#select_Rece_user').jstree(true).get_selected(false);//获得整个树
    $("#otherUserSelect").find("div").each(function () {
        var ids  = $(this).attr("data-id");
        allUser.push(ids);
    });
    var users = allUser.join(",");
    $("#geren").val(users);
    $JuXue.refreshTable("saleCusttransTable");
}
/**擦看详情**/
function viewCusReceDetail(btn){
    var uuid = $(btn).attr("uuid");
    var data = $(btn).attr("data");
    $("#customer_uuid").val(uuid);$("#customer_ids").val(uuid);$("#customer_type").val(data);
    $.post(ctx+"myCustomer/loadCustomerDetail",{type:data},function (r) {
        $("#myCustomer-modal").empty().append(r).modal('show');
    });
}
/**导出转让资料**/
function exportDyTransInfo() {
    var ids = $("#saleCusttransTable").bootstrapTable("getSelections");
    var ids_arr = "";
    if (!ids.length) {$JuXue.n_warning("请勾选需要导出的转让的客户信息！");return;}
    for (var i = 0; i < ids.length; i++) {
        ids_arr += ids[i].dyCusUuid;
        if (i !== (ids.length - 1)) ids_arr += ",";
    }
    $.post(ctx+"cusDynamic/getExAllTranExportData",{"ids":ids_arr},function(r){
        if (r.code === 0) {
            window.open("file/download?fileName=" + r.msg + "&delete=" + true);
        } else {
            $JuXue.n_warning(r.msg);
        }
    });
}